home *** CD-ROM | disk | FTP | other *** search
- Path: news.umbc.edu!not-for-mail
- From: schlein@umbc.edu (Jonas J. Schlein)
- Newsgroups: comp.lang.c
- Subject: Re: Why doesn't this work?
- Date: 7 Mar 1996 18:07:10 -0500
- Organization: University of Maryland Baltimore County
- Message-ID: <4hnq6u$2t3@umbc9.umbc.edu>
- References: <1996Mar4.161412.137442@forest>
- NNTP-Posting-Host: umbc9.umbc.edu
- NNTP-Posting-User: schlein
-
- ebromber@forest.drew.edu> wrote:
- |> Does anyone know why this password program doesn't work properly? And if
- |> you do know the problem how can I fix it? It rejects every password
- |> including the real password. The program was compiled using a MS-DOS
- |> compiler. Thanks in advance.
- |>
- |> main();
-
- Personally I use 'int main (void)' to be explicit, but the semi-colon is
- just plain wrong.
-
- |> { char real[4];
- |> char pass[100];
- |> int count=0;
- |> int i, error;
- |> char c;
-
- This variable should really be an int to comply with getchar().
-
- |> real[0]='j';real[1]='e';real[2]='r';real[3]='k';
- |> printf("PASSWORD: ");
-
- It's customary to #include <stdio.h> when using printf().
-
- |> fflush(stdout);
-
- Same thing with fflush() and stdout!
-
- |> while (c=getch() !='\n')
-
- There is no function getch() in ANSI C. Perhaps you mean getchar()?
- In which case you also should have an extra pair of ()'s like so:
-
- while ((c = getchar()) != '\n')
-
- |> { count++;
-
- Either move count to the bottom of the loop or start it at -1.
-
- |> pass[count]=c;
- |> putch('*');
-
- There is no function putch() in ANSI C. Perhaps you mean putchar()?
-
- |> }
- |> if (count!=4) { printf("\nWRONG PASSWORD\n"); main();}
- |> error=0;
- |> for (i=0; i<4; i++)
- |> if (real[i]=pass[i]) error++;
-
- You realize of course you should be testing inequality. And even if you
- do want to text equality the = operator is not it. That is for assignment!
- In other words your test will always be true unless assigning the
- value 0. I'd change it to:
-
- if (real[i] != pass[i])
-
- |> if (error>0) {printf("\nWRONG PASSWORD\n"); main();}
-
- And we can't forget 'return (0);'. Even if you don't explicitly declare
- main()'s return type it's still int.
-
- |> }
- --
- "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
-
- Jonas J. Schlein (schlein@gl.umbc.edu)
-